package com.jims.master.modules.utils;

import com.jims.master.common.utils.SpringContextHolder;
import com.jims.master.modules.drug.dao.DrugExportMasterDao;
import com.jims.master.modules.drug.dao.DrugImportMasterDao;
import com.jims.master.modules.drug.dao.DrugStorageDeptDao;
import com.jims.master.modules.drug.dao.DrugSupplierCataDao;
import com.jims.master.modules.drug.entity.*;
import com.jims.master.modules.drug.service.DrugBuyPlanService;
import com.jims.master.modules.sys.entity.Dict;
import com.jims.master.modules.sys.utils.DictUtils;
import com.jims.master.modules.sys.utils.UserUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CTQ on 2017-03-14 11:20:04
 */
public class DrugUtils {

    private static DrugStorageDeptDao drugStorageDeptDao = SpringContextHolder.getBean(DrugStorageDeptDao.class);
    private static DrugSupplierCataDao drugSupplierCataDao = SpringContextHolder.getBean(DrugSupplierCataDao.class);
    private static DrugBuyPlanService drugBuyPlanService = SpringContextHolder.getBean(DrugBuyPlanService.class);
    private static DrugImportMasterDao drugImportMasterDao = SpringContextHolder.getBean(DrugImportMasterDao.class);
    private static DrugExportMasterDao drugExportMasterDao = SpringContextHolder.getBean(DrugExportMasterDao.class);
    public static final String CACHE_DICT_MAP = "drugStorageMap";

    public static List<Dict> list = DictUtils.getDictList("DRUG_STOTARG_TYPE");

    public static List<DrugStorageDept> getDrugStorageList(String storageType) {
        DrugStorageDept drugStorageDept = new DrugStorageDept();
        drugStorageDept.updateOrgId();
        drugStorageDept.setStorageType(storageType);
        return drugStorageDeptDao.findList(drugStorageDept);
    }

    /**
     * 批量入库，入库类别过滤返回
     * 药库：退药入库
     * 药房：正常入库、制剂入库、调拨入库
     *
     * @return
     * @author fengyg
     */
    public static List<Dict> getBatchImportClassDictList() {
        DrugStorageDept drugStorageDept = drugStorageDeptDao.getByStorageCode(UserUtils.getUser().getOrgDept().getId());
        List<Dict> newImportClassList = new ArrayList<Dict>();
        if (null != list && list.size() > 0) {
            for (Dict dict : list) {
                if (null != drugStorageDept && drugStorageDept.getStorageType().equals("0")) {  //总药库
                    if (dict.getLabel().equals("退药")) {
                        newImportClassList.add(dict);
                    }
                }
                if (null != drugStorageDept && !drugStorageDept.getStorageType().equals("0")) {  //药房
                    if (dict.getLabel().equals("制剂") || dict.getLabel().equals("调拨") || dict.getLabel().equals("正常")) {
                        newImportClassList.add(dict);
                    }
                }
            }
        }
        return newImportClassList;
    }

    /**
     * 药品入库，入库类别顾虑返回
     * 药库：建账入库、采购入库、制剂入库、盘点入库
     * 药房：建账入库、盘点入库
     *
     * @return
     * @author fengyg
     */
    public static List<Dict> getImportClassDictList() {
        DrugStorageDept drugStorageDept = drugStorageDeptDao.getByStorageCode(UserUtils.getUser().getOrgDept().getId());
        List<Dict> newImportClassList = new ArrayList<Dict>();
        if (null != list && list.size() > 0) {
            for (Dict dict : list) {
                if (dict.getLabel().equals("建账") || dict.getLabel().equals("盘点")) {   //公共
                    newImportClassList.add(dict);
                }
                if (null != drugStorageDept && drugStorageDept.getStorageType().equals("0")) {    //总药库
                    if (dict.getLabel().equals("制剂") || dict.getLabel().equals("采购")) {
                        newImportClassList.add(dict);
                    }
                }
            }
        }
        return newImportClassList;
    }

    /**
     * 药品出库，出库类别过滤返回
     * 药库：发放出库、制剂出库、盘点出库
     * 药房：退药出库、调拨出库、盘点出库
     *
     * @return
     * @author fengyg
     */
    public static List<Dict> getExportClassDictList() {
        DrugStorageDept drugStorageDept = drugStorageDeptDao.getByStorageCode(UserUtils.getUser().getOrgDept().getId());
        List<Dict> newExportClassList = new ArrayList<Dict>();
        if (null != list && list.size() > 0) {
            for (Dict dict : list) {
                if (dict.getLabel().equals("盘点") || dict.getLabel().equals("退药")) {    //公共
                    newExportClassList.add(dict);
                }
                if (null != drugStorageDept && drugStorageDept.getStorageType().equals("0")) {    //总药库
                    if (dict.getLabel().equals("发放") || dict.getLabel().equals("制剂")) {
                        newExportClassList.add(dict);
                    }
                }
                if (null != drugStorageDept && !drugStorageDept.getStorageType().equals("0")) {    //药房
                    if (dict.getLabel().equals("调拨")) {
                        newExportClassList.add(dict);
                    }
                }
            }
        }
        return newExportClassList;
    }

    /**
     * 根据传过来的入出库类型返回供货方集合
     *
     * @param type
     * @return
     * @author fengyg
     */
    public static List getSupplierOrStorageList(String type) {
        DrugStorageDept storage = drugStorageDeptDao.getByStorageCode(UserUtils.getUser().getOrgDept().getId());
        DrugSupplierCata drugSupplierCata = new DrugSupplierCata();
        drugSupplierCata.setDelFlag("0");
        List<DrugSupplierCata> supplierList = drugSupplierCataDao.findList(drugSupplierCata);
        DrugStorageDept drugStorageDept = new DrugStorageDept();
        drugStorageDept.setDelFlag("0");
        List<DrugStorageDept> storageList = drugStorageDeptDao.findList(drugStorageDept);
        if (type.equals("采购") || type.equals("0") || type.equals("退药") || type.equals("4")) {
            if (storage.getStorageType().equals("0")) {   //总药库
                return supplierList;
            } else {
                return storageList;
            }
        } else {
            return storageList;
        }
    }

    /**
     * 根据状态获取采购单据号
     * @param stateFlag 采购单状态
     * @return
     * @author fengyg
     */
    public static List<DrugBuyPlan> getBuyPlanNum(String stateFlag) {
        List<DrugBuyPlan> list = drugBuyPlanService.getBuyPlanNum(stateFlag);
        return list;
    }

    /**
     * 根据ID获取生产商(供应商)
     *
     * @param id
     * @return
     * @author fengyg
     */
    public static String getSupplierById(String id) {
        if (null != id && !id.trim().equals("")) {
            DrugSupplierCata drugSupplierCata=drugSupplierCataDao.get(id);
            if(drugSupplierCata!=null){
                return drugSupplierCata.getSupplierJc();
            }
            else {
                return "";
            }
        } else {
            return "";
        }
    }

    /**
     * 厂家翻译
     *
     * @return
     * @author fengyg
     */
    public static List<DrugSupplierCata> getSupplierList() {
        DrugSupplierCata drugSupplierCata = new DrugSupplierCata();
        drugSupplierCata.setDelFlag("0");
        return drugSupplierCataDao.findList(drugSupplierCata);
    }

    /**
     * 入库详细信息
     *
     * @return
     * @author qml
     */
    public static DrugImportMaster getDrugImportMasterByMasterId(String id) {
        if (null != id && !id.trim().equals("")) {
            DrugImportMaster drugImportMaster=drugImportMasterDao.get(id);
            return drugImportMaster;
        } else {
            return null;
        }
    }
    /**
     * 出库详细信息
     *
     * @return
     * @author qml
     */
    public static DrugExportMaster getDrugExportMasterByMasterId(String id) {
        if (null != id && !id.trim().equals("")) {
            return drugExportMasterDao.get(id);
        } else {
            return null;
        }
    }

}

